package com.example.easyexcel.converters;


import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.converters.ReadConverterContext;
import com.alibaba.excel.converters.WriteConverterContext;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.data.WriteCellData;

/**
 * excel性别转换器
 */
public class GenderConverter implements Converter<Integer> {
    @Override
    public Class<?> supportJavaTypeKey() {
        return Integer.class;//对象属性类型
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;//CellData属性类型
    }

    @Override
    public Integer convertToJavaData(ReadConverterContext<?> context) {
        String cellStr = context.getReadCellData().getStringValue();
        if (StrUtil.equals("男",cellStr) ) {
            return 0;
        } else if (StrUtil.equals("女",cellStr) ) {
            return 1;
        }
        return null;//CellData转对象属性
    }

    @Override
    public WriteCellData<?> convertToExcelData(WriteConverterContext<Integer> context) {
        Integer cellValue = context.getValue();
        if (cellValue != null) {
            if (cellValue == 0) {
                return new WriteCellData<>("男");
            } else if (cellValue == 1) {
                return new WriteCellData<>("女");
            }
        }
        return new WriteCellData<>("");//对象属性转CellData
    }
}